﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bentley.Geometry;
using Bentley.GenerativeComponents;
using Bentley.GenerativeComponents.MicroStation;
using Bentley.GenerativeComponents.Features;
using Bentley.GenerativeComponents.Features.Specific;
using Bentley.GenerativeComponents.GeneralPurpose;
using Bentley.GenerativeComponents.GCScript;
using Bentley.GenerativeComponents.GCScript.GCTypes;
using Bentley.Interop.MicroStationDGN;
using UbiMashGCFeatures;

namespace Bentley.GenerativeComponents.Features  
{
    [GCNamespace("UbiMashFeatures"), HideInheritedUpdateMethods]  
    public class UbiMashPoint : Point, UbiMashListener
    {
        public string pointList;
        private UbiMashSubscriber usub;
        //private UbiMashSubscriberUI ui;

        public UbiMashPoint()
        {
            usub = new UbiMashSubscriber();
            usub.AddListener(this);
            usub.ConnectToUbiMash("127.0.0.1", 10009);
            //ui = new UbiMashSubscriberUI(ref usub);
            //ui.Show();
        }

        public UbiMashPoint(Feature parentFeature)
            : base(parentFeature)
        {
            usub = new UbiMashSubscriber();
            usub.AddListener(this);
            //ui = new UbiMashSubscriberUI(ref usub);
            //ui.Show();
        }

        //public static bool UbiMashVariableSelector(ref string ConsTypeExpression)
        //{
        //    //ConstructionSelectorData csd = new ConstructionSelectorData();
        //    string varName = "";
        //    UbiMashSubscriberUI csForm = new UbiMashSubscriberUI(ref varName);
        //    csForm.ExpressionText = ConsTypeExpression;
        //    DialogResult dr = csForm.ShowDialog();
        //    if (dr == DialogResult.OK)
        //    {
        //        ConsTypeExpression = csForm.ExpressionText;
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        public void refresh(string ptListStr)
        {
            this.pointList = ptListStr;
            this.UpdateSubgraph();
            this.UpdateDisplay();
        }

        public override void Dispose()
        {
            usub.Dispose();
            base.Dispose();
        }
   
        /// <summary>Simple line by two points</summary>
        [Update]
        public bool ByPointList
        (
            FeatureUpdateContext updateContext,
            string UbiMashVariableNameToSubscribeTo,
            //[SpecializedExpressionBuilderAttribute("UbiMashVariableSelector")] string commData,
            //string UbiMashServerIP,
            //int UbiMashServerPort,
            //string UbiMashVariableNameToSubscribeTo,
            [Out] ref string pointListStr
        )
        {
            usub.Subscribe(UbiMashVariableNameToSubscribeTo);
            pointListStr = pointList;

            if (this.ContainsConstituentFeatures())
            {
                this.DeleteConstituentFeatures(updateContext);
                //this.ClearAndErase(updateContext);
            }

            if (!String.IsNullOrEmpty(pointListStr))
            {
                
                string[] pts = pointListStr.Split(';');

                if (pts.Length > 0)
                {
                    //UbiMashPoint topPoint = new UbiMashPoint(this);
                    //topPoint.LetConstituentFeaturesBeDirectlyIndexible();
                    //topPoint.AlignOptions(this);

                    double[][] ptList = new double[pts.Length][];

                    if (pts[pts.Length - 1] == "")
                    {
                        ptList = new double[pts.Length - 1][];
                    }

                    for (int i = 0; i < ptList.Length; i++)
                    {
                        string[] xyz = pts[i].Split(':');
                        if (xyz.Length == 3)
                        {
                            try
                            {
                                ptList[i] = new double[3];
                                ptList[i][0] = Convert.ToDouble(xyz[0]);
                                ptList[i][1] = Convert.ToDouble(xyz[1]);
                                ptList[i][2] = Convert.ToDouble(xyz[2]);

                                DPoint3d pt3d = new DPoint3d();

                                pt3d = new DPoint3d(ptList[i][0], ptList[i][1], ptList[i][2]);
                                UbiMashPoint pt = new UbiMashPoint(this);
                                pt.LetConstituentFeaturesBeDirectlyIndexible();
                                pt.FromDPoint3d(pt3d);
                                pt.DenoteAsHavingBeenUpdated();
                                pt.AlignOptions(this);
                                pt.SetSuccess(true);

                                this.AddConstituentFeature(pt);
                            }
                            catch (Exception castex)
                            {
                                MessageBox.Show(castex.Message);
                            }
                        }
                    }
                    base.ConstructionsVisible = true;
                }
                
            }
            return true;  // Return true to indicate that this feature was updated successfully.
        }

    } // class

} // namespace

